﻿/*
 * Project: IV:MP GUI Editor (http://code.google.com/p/ivmp-gui-editor)
 * Code license: GNU Lesser GPL (http://www.gnu.org/copyleft/lesser.html)
 * 
 * Authors: CrackHD
 * Description: A general IDE ETab class, which represents any opened tab in editor's window. 
 * Tabs can containt different types of content, look and act different.
 */

using System;
using System.IO;
using System.Windows.Controls;
using System.Windows;

namespace IvmpEditor
{
    public interface ITab
    {
        // Interface which every Tab.Content supports:

        //bool IsChangeable { get; set; }
        bool IsSaved { get; set; }
        bool IsDesiredNew { get; set; }
        string Name { get; }
        void Focus();
        void SetupEvents();
        bool Save();
        bool ShowSaveAsDialog();//todo
    }

    public class ETab
    {
        // Definition of ETab class, represents every TabPage
        public enum ETabType { Document, GuiDesignerWorkbench };

        // Fields:
        public TabItem         TabPage;
        private TextBlock      tabPageHeader;
        public Editor           Editor;
        public ITab            Content;

        // Tab construction in different modes:
        public static ETab OpenFile(Editor editor, FileInfo file, DocumentFileExtension desiredType = null, string name = "New")
        {
            var tab = new ETab();
            tab.Editor = editor;

            // Create tab with header:
            tab.TabPage = new TabItem();
            tab.tabPageHeader = new TextBlock();
            tab.TabPage.Header = tab.tabPageHeader;
            tab.tabPageHeader.ContextMenu = editor.MainWindow.Resources["MenuTabPage"] as ContextMenu;

            // Detect a type of this document:
            if (file == null) // create "new" file
            {
                if (desiredType.HideInSaveAsDialog)
                {
                    if (desiredType == SupportedFileTypes.IvmpGuiWindow)
                    {
                        tab.Content = new GuiDesignerControl(tab, "Window1");// todo: unique ID for each window
                        tab.TabHeader = tab.GuiDesigner.GetName();
                        tab.TabPage.Content = tab.GuiDesigner.Workbench;
                    }
                    else
                        throw new NotSupportedException("FIXME: Create in special mode: " + desiredType.Name);
                }
                else
                {
                    // Create "new" script/text file (EDocument)
                    tab.Content = new EDocument(tab, null, desiredType, name);
                    tab.TabHeader = tab.Document.File.Name;
                    tab.TabPage.Content = tab.Document.TextBox;
                }
            }
            else // Open "existing" file
            {
                var docType = SupportedFileTypes.GetByExtension(file.Extension);
                if (docType.HideInSaveAsDialog)
                {
                    if (docType == SupportedFileTypes.IvmpGuiWindow)
                        throw new NotImplementedException("TODO");//Load Gui editor workbench from file
                    else
                        throw new NotSupportedException("FIXME: Open in special mode: " + desiredType.Name);
                }
                else
                {
                    // Load trivial EDocument (script/text file)
                    tab.Content = new EDocument(tab, file);
                    tab.TabHeader = tab.Document.File.Name;
                    tab.TabPage.Content = tab.Document.TextBox;
                }
            }
            return tab;
        }
        public static ETab CreateNew(Editor editor, DocumentFileExtension desiredType = null, string name = "New")
        {
            if (desiredType == null)
                desiredType = SupportedFileTypes.Squirrel;//todo, "New file..." dialog!

            return OpenFile(editor, null, desiredType, name);
        }

        // Properties:
        public string TabHeader
        {
            // Gets or sets a header text of this TabPage
            get { return tabPageHeader.Text; }
            set { tabPageHeader.Text = value; }
        }
        public bool IsDocument
        {
            // Returns true if this tab contains ETab.EDocument inside
            get { return Content is EDocument; }
        }
        public bool IsGuiDesigner
        {
            // Returns true if this tab contains GUI Designer Workbench
            get { return Content is GuiDesignerControl; }
        }
        public EDocument Document
        {
            // Returns content as document
            get { return Content as EDocument; }
        }
        public GuiDesignerControl GuiDesigner
        {
            // Returns content as GUI workbench/designer:
            get { return Content as GuiDesignerControl; }
        }

        // Methods:
        public void SetUpEvents()
        {
            // Sets an events for this tab and its content which ensures correct behavior
            tabPageHeader.MouseRightButtonDown +=
                delegate { Focus(); };

            Content.SetupEvents();
        }
        public void Focus()
        {
            // Focuses tab and its content
            TabPage.IsSelected = true;
            Content.Focus();
        }
        public bool Save()
        {
        _do:
            try
            {
                return Content.Save();
            }
            catch (Exception ex)
            {
                // fixme localize
                if (MessageBox.Show(string.Format("Failed to save: {0}{1}{1}Retry?", ex.Message, Environment.NewLine), 
                    "Save: error", MessageBoxButton.YesNo, MessageBoxImage.Stop) == MessageBoxResult.Yes)
                    goto _do;
                return false;
            }
        }
    }
}
